package com.project.web.controller.user;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.project.common.constant.ShiroConstants;
import com.project.common.utils.TimeUuidUtil;
import com.project.system.service.IGoodsInfoService;
import com.project.user.domain.UserAddress;
import com.project.user.domain.UserCart;
import com.project.user.domain.UserOrderDetail;
import com.project.user.service.IUserAddressService;
import com.project.user.service.IUserCartService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.project.user.domain.UserOrder;
import com.project.user.service.IUserOrderService;
import com.project.web.controller.common.BaseController;
import com.project.common.base.TableDataInfo;
import com.project.common.base.AjaxResult;

import javax.servlet.http.HttpServletRequest;

/**
 * 订单列 信息操作处理
 *
 * @author administrator
 */
@Controller
@RequestMapping("/user/userOrder")
public class UserOrderController extends BaseController {
    private String prefix = "normal";

    @Autowired
    private IUserOrderService userOrderService;
    @Autowired
    private IGoodsInfoService goodsInfoService;
    @Autowired
    private IUserAddressService userAddressService;
    @Autowired
    private IUserCartService userCartService;

    /**
     * 购买跳转确认订单页面
     *
     * @param goodsId
     * @param goodsNum
     * @param isFromCart
     * @param modelMap
     * @return
     */
    @RequestMapping("/placeOrder")
    public String placeOrder(Long[] goodsId, Integer[] goodsNum, Integer isFromCart, ModelMap modelMap) {
        List<UserCart> list = new ArrayList<>();
        for (int i = 0; i < goodsId.length; i++) {
            UserCart userCart = new UserCart();
            userCart.setGoodsNum(goodsNum[i]);
            userCart.setGoodsId(goodsId[i]);
            userCart.setGoodsInfo(goodsInfoService.selectGoodsInfoById(goodsId[i]));
            list.add(userCart);
        }
        modelMap.put("topTitle", "确认订单");
        modelMap.put("dataList", list);
        modelMap.put("addrList", userAddressService.selectUserAddressList(new UserAddress(this.getUserId())));
        modelMap.put("isFromCart", isFromCart);
        modelMap.put("isvip", this.getUser().getIsvip());
        return prefix + "/place_order";
    }

    /**
     * 新增保存订单列
     *
     * @param userOrder
     * @param goodsId
     * @param goodsPrice
     * @param goodsNum
     * @param isFromCart
     * @param goodsName
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserOrder userOrder, Long[] goodsId, Double[] goodsPrice, Integer[] goodsNum, Integer isFromCart, String[] goodsName, HttpServletRequest request) {
        long orderId = TimeUuidUtil.get16UUID();
        List<UserOrderDetail> details = new ArrayList<>();
        Double goodsLeft = null;
        for (int i = 0; i < goodsId.length; i++) {
            UserOrderDetail detail = new UserOrderDetail(orderId, goodsId[i], goodsNum[i]);
            detail.setGoodsPrice(goodsPrice[i]);
            details.add(detail);
            goodsLeft = goodsInfoService.queryGoodsLeft(goodsId[i]);
            if (goodsLeft < goodsNum[i] * 1d) {
                return error("其中物品【" + goodsName[i] + "】的数量只有" + goodsLeft + "，请减少购买该物品的数量。");
            }
        }

        userOrder.setUserId(this.getUserId());
        userOrder.setStatus(4);
        userOrder.setDetails(details);
        userOrderService.insertUserOrder(userOrder, isFromCart, this.getUser().getIsvip());
        request.getSession().setAttribute(ShiroConstants.CURRENT_CART_NUM, userCartService.countUserCart(this.getUserId()));
        return null;
    }

    /**
     * 查询订单
     *
     * @param userOrder
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserOrder userOrder) {
        if (userOrder == null) {
            userOrder = new UserOrder();
        }
        userOrder.setUserId(this.getUserId());
        startPage();
        List<UserOrder> orderList = userOrderService.selectUserOrderList(userOrder);
        return getDataTable(orderList);
    }

    /**
     * 修改保存订单列
     *
     * @param userOrder
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult edit(UserOrder userOrder) {
        return toAjax(userOrderService.updateUserOrder(userOrder));
    }

    /**
     * 修改保存订单列
     *
     * @param userOrder
     * @return
     */
    @PostMapping("/editStatus")
    @ResponseBody
    public AjaxResult editStatus(UserOrder userOrder) {
        return toAjax(userOrderService.updateUserOrderStatus(userOrder));
    }

    /**
     * 删除订单列
     *
     * @param orderId
     * @return
     */
    @GetMapping("/remove")
    public String remove(Long orderId) {
        userOrderService.deleteUserOrderById(orderId);
        return "redirect:/user/order";
    }

    /**
     * 查看订单详情
     *
     * @param orderId
     * @param mmap
     * @return
     */
    @GetMapping("/view/{orderId}")
    public String userGoodsRemark(@PathVariable("orderId") Long orderId, ModelMap mmap) {
        mmap.put("order", userOrderService.selectUserOrderById(orderId));
        mmap.put("menuType", "order");
        return prefix + "/user_order_detail";
    }

    /**
     * 申请退款
     *
     * @param orderId
     * @param modelMap
     * @return
     */
    @GetMapping("/refund")
    public String refund(Long orderId, ModelMap modelMap) {
        modelMap.put("orderId", orderId);
        return "normal/user_refund";
    }

    /**
     * 处理退款
     *
     * @param orderId
     * @param modelMap
     * @return
     */
    @GetMapping("/handleRefund")
    public String handleRefund(Long orderId, ModelMap modelMap) {
        modelMap.put("order", userOrderService.selectUserOrderById(orderId));
        return "system/order/handleRefund";
    }

    /**
     * 退款查询
     *
     * @param
     * @param
     * @return
     */
    @RequiresPermissions("system:order:orderRefund")
    @GetMapping("/refundList")
    public String refundList() {
        return "system/order/orderRefund";
    }

    /**
     * 跳转页面
     *
     * @return
     */
    @RequiresPermissions("system:order:orderSend")
    @GetMapping("/orderSend")
    public String orderSend() {
        return "system/order/orderSend";
    }

    /**
     * 通过菜品分类订单统计
     *
     * @param mmap
     * @return
     */
    @RequiresPermissions("system:order:statistics1")
    @GetMapping("/statistics1")
    public String statistics1(ModelMap mmap) {
        mmap.put("data", userOrderService.countByGoodsId());
        return "/system/statistics/statistics1";
    }

    /**
     * 跳转统计页面
     *
     * @return
     */
    @RequiresPermissions("system:order:statistics2")
    @GetMapping("/statistics2")
    public String statistics2() {
        return "/system/statistics/statistics2";
    }

    /**
     * 统计金额
     *
     * @param userOrder
     * @return
     */
    @PostMapping("/statistics2")
    @ResponseBody
    public List<Map<String, Object>> statistics2(UserOrder userOrder) {
        return userOrderService.countMoneyByQuetar(userOrder);
    }
}
